package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.adapter.InchingChangeTextColorAdapter;
import itbour.onetouchshow.adapter.InchingFontListAdapter;
import itbour.onetouchshow.base.SPCTag;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.bean.inching.noppt.InchingOutLineTextOrShapeBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;
import itbour.onetouchshow.utils.CommonUtils;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.SpUtils;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.widget.ClearEditTextView;

/**
 * Created by zjl_d on 2017/11/16.
 * <p>
 * 本类为微调文字操作面板类
 */

public class InchingTextOperaPanel extends LinearLayout {

    private LinearLayout wenben;
    private LinearLayout yanse;
    private LinearLayout ziti;
    private LinearLayout yangshi;
    private LinearLayout editBar;

    private RelativeLayout opearDetail;

    private RelativeLayout onWenBen;
    private RelativeLayout onColor;
    private RelativeLayout onType;
    private RelativeLayout onYangShi;
    private ClearEditTextView etContent;
    private TextView tvEnsure;
    //用于当前选中的标签是文本还是颜色 还是字体还是样式
    private String nowSelect = "";
    private InchingChangeTextColorAdapter rvColorAdapter;
    private InchingFontListAdapter inchingFontListAdapter;
    private ImageView wenbenImg;
    private TextView wenbenTv;
    private ImageView yanseImg;
    private TextView yanseTv;
    private ImageView zitiImg;
    private TextView zitiTv;
    private ImageView yangshiImg;
    private TextView yangshiTv;
    private SeekBar textSizeBar;
    private SeekBar lineSpanBar;
    private SeekBar textSpanBar;

    /*除字体以外面板的高度*/
    private int changePanleHeiht;
    private Context context;
    private OnPanelButtonClick onPanelButtonClick;
    private InchingOutLineTextOrShapeBean textDaGangData;
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String string = s.toString();
            //etContent.setSelection(s.length());
            String nowString = textDaGangData.getTe().toString();
            if (nowString == null) {

                onPanelButtonClick.onEditTextChange(string);
            } else if (!nowString.equals(string)) {
                onPanelButtonClick.onEditTextChange(string);

            }

        }
    };


    public InchingTextOperaPanel(Context context) {
        this(context, null);
    }

    public InchingTextOperaPanel(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InchingTextOperaPanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        //实现该接口的activity 使用该界面的activity
        onPanelButtonClick = (OnPanelButtonClick) context;

    }

    public InchingFontListAdapter getInchingFontListAdapter() {
        return inchingFontListAdapter;
    }

    public RelativeLayout getOnWenBen() {
        return onWenBen;
    }

    public boolean editBarIsShow() {
        return UIUtils.viewIsVisable(editBar);
    }

    public void setWenBenParams(int height) {

        ViewGroup.LayoutParams layoutParams = onWenBen.getLayoutParams();
        layoutParams.height = height;
        onWenBen.setLayoutParams(layoutParams);
    }

    public int getWenBenHeight() {
        int height = onWenBen.getHeight();
        return height;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //初始化view
        findView();
        //初始化监听
        initClickListener();
    }

    private void hidAllOpearPanel() {
        onWenBen.setVisibility(GONE);
        onColor.setVisibility(GONE);
        onType.setVisibility(GONE);
        onYangShi.setVisibility(GONE);


        wenbenImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_op_un_select));
        yanseImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_color_op_un_select));
        yangshiImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_yangshi_op_un_select));
        zitiImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_ziti_op_un_select));

        wenbenTv.setTextColor(Color.parseColor("#B5B5B5"));
        yanseTv.setTextColor(Color.parseColor("#B5B5B5"));
        yangshiTv.setTextColor(Color.parseColor("#B5B5B5"));
        zitiTv.setTextColor(Color.parseColor("#B5B5B5"));

    }

    private void initClickListener() {
        wenben.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                wenbenTabClick();

            }
        });
        yanse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                yanseTabClick();
            }
        });

        ziti.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                zitiTabClick();
            }

        });

        yangshi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                yangshiTabClick();
            }
        });


        tvEnsure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hidePanelAndKeyBroad();
                onPanelButtonClick.onEnsure();
            }
        });
    }

    public void yangshiTabClick() {
        if (!nowSelect.equals(AppConst.YANGSHI)) {
            if (nowSelect.equals(AppConst.WENBEN)) {
                nowSelect = AppConst.WAITYANGSHI;
                UIUtils.hideKeyboard(etContent);
            } else {
                onYangShi(etContent);
            }
        }
    }

    public void zitiTabClick() {
        if (!nowSelect.equals(AppConst.ZITI)) {
            if (nowSelect.equals(AppConst.WENBEN)) {
                nowSelect = AppConst.WAITZITI;
                UIUtils.hideKeyboard(etContent);
            } else {
                onZiTi(etContent);
            }
        }
    }

    public void yanseTabClick() {
        if (!nowSelect.equals(AppConst.YANSE)) {
            //如果是从文本切换而来
            if (nowSelect.equals(AppConst.WENBEN)) {
                nowSelect = AppConst.WAITYANSE;
                UIUtils.hideKeyboard(etContent);
            } else {
                //需要隐藏键盘时 先隐藏键盘 再改变布局
                onYanSe(etContent);
            }


        }
    }

    public void wenbenTabClick() {
        if (!nowSelect.equals(AppConst.WENBEN)) {
            //需要弹出键盘时
            //先改变布局 再切换键盘的状态
            onWenBen();

            L_.i("wenbenTabClick");


            CommonUtils.setEditTextFilters(etContent, 100);
            String value = textDaGangData.getTe();

            String text = etContent.getText().toString();


            if (text == null) {
//                etContent.setText(value);
//                etContent.setSelection(value.length());
            } else if (!text.equals(value)) {
//                etContent.setText(value);
//                etContent.setSelection(value.length());
            }
            etContent.setText(value);
            if (value.length() >= 100) {
                etContent.setSelection(100);
            } else {
                etContent.setSelection(value.length());
            }
//         etContent.removeTextChangedListener(textWatcher);
        }
    }

    /**
     * 删除 确认 放大后的界面 收起键盘和整个操作面板
     */
    public void hidePanelAndKeyBroad() {
        UIUtils.hideKeyboard(etContent);
        this.setVisibility(GONE);
        nowSelect = "";
        if (onPanelButtonClick != null) {

            onPanelButtonClick.onPanelHide();
        }
    }

    private void onYangShi(View view) {
        UIUtils.hideKeyboard(view);

        hidAllOpearPanel();
        yangshiImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_yangshi_op_on_select));
        yangshiTv.setTextColor(Color.parseColor("#FF19E9E9"));

        editBar.setVisibility(GONE);
        onYangShi.setVisibility(VISIBLE);
        nowSelect = AppConst.YANGSHI;


    }

    private void onZiTi(View view) {
        UIUtils.hideKeyboard(view);

        hidAllOpearPanel();
        zitiImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_ziti_op_on_select));
        zitiTv.setTextColor(Color.parseColor("#FF19E9E9"));
        editBar.setVisibility(GONE);
        onType.setVisibility(VISIBLE);
        nowSelect = AppConst.ZITI;

    }

    private void onYanSe(View view) {
        UIUtils.hideKeyboard(view);

        hidAllOpearPanel();

        yanseImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_color_op_on_select));
        yanseTv.setTextColor(Color.parseColor("#FF19E9E9"));

        editBar.setVisibility(GONE);
        onColor.setVisibility(VISIBLE);
        nowSelect = AppConst.YANSE;
    }

    //用于UI展示当前为文本状态
    public void onWenBen() {
        hidAllOpearPanel();
        wenbenImg.setImageDrawable(getResources().getDrawable(R.drawable.iv_inching_text_op_on_select));
        wenbenTv.setTextColor(Color.parseColor("#FF19E9E9"));
        onWenBen.setVisibility(INVISIBLE);
        editBar.setVisibility(VISIBLE);
        UIUtils.showKeyboard(etContent);
        nowSelect = AppConst.WENBEN;
    }


    public void setThisShow() {
        this.setVisibility(VISIBLE);
        //将本布局的层级调到最前 在印品会遮挡制作完成
        this.bringToFront();

    }

    private void findView() {
        //文本按钮
        wenben = findViewById(R.id.ll_inching_text_content);
        wenbenImg = findViewById(R.id.iv_inching_text_content);
        wenbenTv = findViewById(R.id.tv_inching_text_content);

        //颜色按钮
        yanse = findViewById(R.id.ll_inching_text_color);
        yanseImg = findViewById(R.id.iv_inching_text_color);
        yanseTv = findViewById(R.id.tv_inching_text_color);

        //字体按钮
        ziti = findViewById(R.id.ll_inching_text_style);
        zitiImg = findViewById(R.id.iv_inching_text_style);
        zitiTv = findViewById(R.id.tv_inching_text_style);
        //样式按钮
        yangshi = findViewById(R.id.ll_inching_text_type);
        yangshiImg = findViewById(R.id.iv_inching_text_type);
        yangshiTv = findViewById(R.id.tv_inching_text_type);

        //输入文字框整体
        editBar = findViewById(R.id.ll_edit_bar);
        etContent = findViewById(R.id.et_content);
        etContent.addTextChangedListener(textWatcher);

        tvEnsure = findViewById(R.id.tv_ensure);


        //操作的详细界面
        opearDetail = findViewById(R.id.rl_opear_detail_content);

        //输入详情的布局
        onWenBen = findViewById(R.id.rl_on_text);
        //默认高度为0 如果这个高度小于300 那么认为

        //如果之前获取到过键盘高那么直接设置
        int height = SpUtils.getInt(SPCTag.KEYBROAD_HEIGHT, 0);
        if (height > 0 && height > 300) {
            if (onWenBen.getHeight() != height) {
                //如果没有在微调界面的监听里会去设置
                setWenBenParams(height);
            }

        }


        /*根据第一个Color计算面板高度确定以后所有面板高度*/
        changePanleHeiht = ((UIUtils.WHD()[0] - UIUtils.dp2px(144)) / 6 + UIUtils.dp2px(2) + UIUtils.dp2px(20)) * 4;
        onColor = findViewById(R.id.rl_on_color);
        initOnColor();


        onType = findViewById(R.id.rl_type);
        initType();


        onYangShi = findViewById(R.id.rl_style);
        onYangShi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                L_.i("onYangShiOnClick");
            }
        });
        initYangShi();
    }

    private void initYangShi() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View yangshiView = inflater.inflate(R.layout.layout_panel_text_yangshi, onYangShi);
        //设置面板高度
        ViewGroup.LayoutParams layoutParams = yangshiView.getLayoutParams();
        layoutParams.height = changePanleHeiht;
        textSizeBar = yangshiView.findViewById(R.id.sb_text_size_bar);
        lineSpanBar = yangshiView.findViewById(R.id.sb_line_span_bar);
        textSpanBar = yangshiView.findViewById(R.id.sb_text_span_bar);


        textSizeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (textSizeBar != null && textSpanBar != null && lineSpanBar != null) {
                    if (onPanelButtonClick != null) {
                        onPanelButtonClick.onSeekBarChange(textSizeBar.getProgress(), lineSpanBar.getProgress(), textSpanBar.getProgress());

                    }
                }
            }
        });

        lineSpanBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (textSizeBar != null && textSpanBar != null && lineSpanBar != null) {
                    if (onPanelButtonClick != null) {
                        onPanelButtonClick.onSeekBarChange(textSizeBar.getProgress(), lineSpanBar.getProgress(), textSpanBar.getProgress());

                    }
                }


            }
        });
        textSpanBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (textSizeBar != null && textSpanBar != null && lineSpanBar != null) {
                    if (onPanelButtonClick != null) {
                        onPanelButtonClick.onSeekBarChange(textSizeBar.getProgress(), lineSpanBar.getProgress(), textSpanBar.getProgress());

                    }
                }
            }
        });


    }

    private void initType() {
        ViewGroup.LayoutParams layoutParams = onType.getLayoutParams();
        layoutParams.height = changePanleHeiht;
        List<HomeDetailslBean.FontListBean> fontList = InchingDataSingleCase.INSTANCE.getFontList();
        RecyclerView rvType = new RecyclerView(context);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        rvType.setLayoutManager(layoutManager);
        inchingFontListAdapter = new InchingFontListAdapter(context, fontList);
        rvType.setAdapter(inchingFontListAdapter);
        inchingFontListAdapter.setFontStyleListener(new InchingFontListAdapter.FontStyleListener() {
            @Override
            public void onItemClick(int position) {
                L_.i("setFontStyleListener===" + position);
                if (onPanelButtonClick != null) {
                    onPanelButtonClick.onTypeClick(position);
                }

            }
        });
        onType.addView(rvType);

    }

    private void initOnColor() {
        RecyclerView rvColor = new RecyclerView(context);

        List<List<Integer>> colorList = InchingDataSingleCase.INSTANCE.getColorList();

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 6);
        rvColor.setLayoutManager(gridLayoutManager);
        rvColorAdapter = new InchingChangeTextColorAdapter(context, colorList);
        rvColor.setAdapter(rvColorAdapter);
        onColor.addView(rvColor);
        ((DefaultItemAnimator) rvColor.getItemAnimator()).setSupportsChangeAnimations(false);

        rvColorAdapter.setOnTextColorItemClick(new InchingChangeTextColorAdapter.OnTextColorItemClick() {
            @Override
            public void onTextColorClick(int r, int g, int b, String type) {
                onPanelButtonClick.onColorClick(r, g, b, type);

            }
        });
    }

    public void setDaGangData(InchingOutLineTextOrShapeBean textDaGangData) {
        this.textDaGangData = textDaGangData;
        if (textSizeBar != null) {
            int fs = (int) (textDaGangData.getFs() * 100);
            fs = fs > 100 ? 100 : fs;
            fs = fs < 0 ? 0 : fs;
            textSizeBar.setProgress(fs);
            L_.i("textSizeBar===" + fs);


        }
        if (textSpanBar != null) {
            int cs = (int) (textDaGangData.getCs() * 100);
            cs = cs > 100 ? 100 : cs;
            cs = cs < 0 ? 0 : cs;
            textSpanBar.setProgress(cs);
            L_.i("textSpanBar===" + cs);

        }
        if (lineSpanBar != null) {
            int ls = (int) (textDaGangData.getLs() * 100);
            L_.i("lineSpanBar===" + ls);
            ls = ls > 100 ? 100 : ls;
            ls = ls < 0 ? 0 : ls;
            lineSpanBar.setProgress(ls);
        }

    }

    public InchingOutLineTextOrShapeBean getTextDaGangData() {
        return textDaGangData;
    }

    public void setNowSelectType(String font) {
        if (inchingFontListAdapter != null) {
            inchingFontListAdapter.setSelectFn(font);
        }
    }

    public void fromWenBenOnKeyBroadDismiss() {
        L_.i("switch1fromWenBenOnKeyBroadDismiss===" + nowSelect);
        if (nowSelect.equals(AppConst.WAITYANSE)) {
            onYanSe(null);
        } else if (nowSelect.equals(AppConst.WAITZITI)) {
            onZiTi(null);
        } else if (nowSelect.equals(AppConst.WAITYANGSHI)) {
            onYangShi(null);
        }
    }

    public boolean needDismiss() {
        return nowSelect.equals(AppConst.WENBEN);
    }

    public interface OnPanelButtonClick {
        void onEnsure();


        void onEditTextChange(String string);

        void onColorClick(int r, int g, int b, String type);

        void onTypeClick(int position);


        void onSeekBarChange(int fs, int ls, int cs);

        //当文字操作面板隐藏的时候
        void onPanelHide();

    }
}
